package wwy.Common;

import org.modelmapper.ModelMapper;
import wwy.Common.orgEntity.LogicEntity;
import wwy.utils.UpdateTool;

import java.io.Serializable;
import java.util.List;

public class LogicService<D extends LogicDao<T,ID>,T extends LogicEntity, ID extends Serializable>{

    protected D dao;
    protected ModelMapper modelMapper = new ModelMapper();
    public LogicService(D lr){
        this.dao = lr;
    }
    protected D getDAO(){
        return dao;
    }

    public T  GET(ID id){
        return  dao.getReferenceById(id);
    }
    public List<T> getAll(){
        List<T> all = dao.findAll();
        all.removeIf(t -> t.getIsDeleted() == 1);
        return all;
    }
    public T  PUT(T entity, T newEntity){
       UpdateTool.copyNullProperties(entity,newEntity);
       return dao.save(entity);
    }
    public T  POST(T  entity){
        return dao.save(entity);
    }
    public void DELETE(T entity){
        dao.delete(entity);
    }
    public void DELETE(ID id){
        dao.deleteById(id);
    }

}
